﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using DG.Tweening;
namespace game
{
    public class CellPrefab : MonoBehaviour ,IBeginDragHandler,IDragHandler,IEndDragHandler
    {
        public int Type
        {
            get;
            set;
        }

        public int State
        {
            get;
            set;
        }

        public bool isIdel { get { return state == CellState.Idel; } }

        public bool IsEmpty { get { return bandingElement == null; } }

        public int X
        {
            get;
            private set;
        }
        public int Y
        {
            get;
            private set;
        }

        public int IndexInPanel
        {
            get { return Y * LevelDatabase.Instance.colCount + X; }
        }

        public Element bandingElement
        {
            get;
            private set;
        }

        Vector2 BeginDragPos;
        bool OnDraging;
        CellState state = CellState.Idel;
        event Action<CellPrefab, Vector2> OnCellDragEvent;

        public void RegisterEvent(Action<CellPrefab, Vector2> _call)
        {
            OnCellDragEvent += _call;
        }

        public void UnRegisterEvent(Action<CellPrefab, Vector2> _call)
        {
            OnCellDragEvent -= _call;
        }

        public void bindElement(Element _ele)
        {
            bandingElement = _ele;
            bandingElement.transform.position = transform.position;
        }

        public void SwapCellElement(CellPrefab _cell)
        {
            var _old = _cell.bandingElement;
            _cell.SwapElement(bandingElement);
            SwapElement(_old);

            state = CellState.Swap;
        }

        public void SwapElement(Element _ele)
        {
            bandingElement.transform.DOMove(_ele.transform.position, CellDatabase.Instance.CellDragSwapDuration).OnComplete(StateToIdel);
            bandingElement = _ele;
        }

        public void FallElement(CellPrefab _other)
        {
            bandingElement = _other.bandingElement;
            _other.bandingElement = null;
            DropElement(bandingElement);
        }
        public void FallElement(Element _ele)
        {
            bandingElement = _ele;
            bandingElement.OnElementCreate(X, Y);
            DropElement(bandingElement);
        }

        public void DropElement(Element _ele)
        {
            state = CellState.Fall;
            bandingElement.transform.DOMove(transform.position, CellDatabase.Instance.CellFallDuration)/*.SetSpeedBased()*/.OnComplete(StateToIdel);
        }

        void StateToIdel()
        {
            state = CellState.Idel;
        }

        public void OnCellCreate(int _x, int _y, Vector2 _pos)
        {
            X = _x;
            Y = _y;
            gameObject.name = "Cell_" + X + "_" + Y;
            gameObject.transform.position = _pos;
            if ((X + Y) % 2 == 0)
                gameObject.GetComponent<SpriteRenderer>().color = new Color(1, 1, 1, 0.5f);
        }

        public void OnBeginDrag(PointerEventData eventData)
        {     
            BeginDragPos = eventData.position;         
        }

        public void OnDrag(PointerEventData eventData)
        {
            if (OnDraging || !isIdel) return;
            Vector2 _dirty = eventData.position - BeginDragPos;
            if (_dirty.sqrMagnitude > CellDatabase.Instance.cellDragZone)
            {              
                OnCellDragEvent(this, _dirty);
                OnDraging = true;
            }
        }

        public void OnEndDrag(PointerEventData eventData)
        {        
            OnDraging = false;
        }

        public void Recycle()
        {
            state = CellState.Rectcle;

            bandingElement.transform.DOScale(0, CellDatabase.Instance.ElementRecycle).OnComplete(() =>
            {
                ElementDatabase.Instance.RecycleElement(bandingElement);
                bandingElement = null;
                state = CellState.Idel;
            });            
        }
    }
}

